#include <stdio.h>
#include <stdlib.h>
#define ElementType int
#define MAX_INPUT 100 // 最大输入数量
typedef struct Node{
    ElementType Element;
    struct Node* Next;
}Node;

typedef Node* Position;
void InitSingleLinkList(Position* head);
/*
实现是次要的，明白原理是主要的
*/



void InitSingleLinkList(Position* head){
    printf("开始初始化链表头节点\n");
    *head = (Position)malloc(sizeof(Node));
    (*head)->Element = 0;
    (*head)->Next = NULL;
} //使得指针有所指

void Insert(Position head) {
    printf("You selected function 2\n");
    int inputArray[MAX_INPUT];
    int input;
    int count = 0;

    while (1) {
        printf("Enter data (press 9 to stop): ");
        if (scanf("%d", &input) != 1) {
            printf("Invalid input. Please enter a number.\n");
            // 清除输入缓冲区
            while (getchar() != '\n');
            continue;
        }
        if (input == 9) {
            printf("Stopping data input.\n");
            break;
        } else {
            if (count < MAX_INPUT) {
                inputArray[count++] = input;
                printf("You entered: %d\n", input);
            } else {
                printf("Input array is full.\n");
                break;
            }
        }
        // 清除输入缓冲区
        while (getchar() != '\n');
    }

    // 输出存储的数据
    printf("Stored data:\n");
    for (int i = 0; i < count; i++) {
        printf("%d ", inputArray[i]);
    }
    printf("\n");

    Position current = head;

    for(int i = 0; i < count;i++) //遍历数组，插入到链表中
    {
        while (current->Next != NULL)
         {
        current = current->Next;
        } 
    Position temp = (Position)malloc(sizeof(Node));
    if (temp == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }
    temp->Element = inputArray[i];
    temp->Next = NULL;
    current->Next = temp;
    }
}
/*--------------GPT 40*/
Position FindPrevious(Position head, ElementType x) {
    Position current = head;
    while (current->Next != NULL && current->Next->Element != x) {
        current = current->Next;
    }
    if (current->Next == NULL) {
        return NULL; // 没有找到
    }
    return current; // 找到的前驱节点
}

int removeElement(Position head, ElementType x) {
    printf("删除元素%d\n",x);
    Position previous = FindPrevious(head, x);
    if (previous == NULL || previous->Next == NULL) {
        printf("No such element\n");
        return 0; // 没有找到要删除的元素
    }

    Position temp = previous->Next;
    previous->Next = temp->Next;
    free(temp);
    return 1;
}
/*-------------------------GPT 40*/
void Traverse(Position head){
    printf("\033[31m开始遍历列表.\033[0m\n"); // 红色
    Position current = head->Next;
    while(current->Next != NULL)
    {
        printf("Data:%d\n",current->Element);
        current = current->Next;
    }
    printf("The Last Element %d\n",current->Element);
}
int main(){
    Position head;
    // InitSingleLinkList(&head);
    // Insert(head,1);
    // Insert(head,2);
    // Insert(head,3);
    // Insert(head,4);
    // Insert(head,5);
    // removeElement(head,5);
    // Traverse(head);
    int choice;

    while (1) {
        // 显示菜单
        printf("Menu:\n");
        printf("1. Init  1\n");
        printf("2. Insert  2\n");
        printf("3. remove 3\n");
        printf("4. search 4\n");
        printf("5. Traverse 5\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        
        // 读取用户输入
        if (scanf("%d", &choice) != 1) {
            printf("Invalid input. Please enter a number.\n");
            // 清除输入缓冲区
            while (getchar() != '\n');
            continue;
        }

        // 根据用户输入调用相应函数
        switch (choice) {
            case 1:
                InitSingleLinkList(&head);
                break;
            case 2:
                Insert(head);
                break;
            // case 3:
            //     // function3();
            //     break;
            case 5:
                Traverse(head);
                break;
            case 6:
                printf("Exiting program.\n");
                exit(0);
            default:
                printf("Invalid choice. Please enter a number between 1 and 5.\n");
        }

        // 清除输入缓冲区
        while (getchar() != '\n');
    }

    return 0;
}